package dav.mobile.garuda.states.game.objects.encounters;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

import dav.mobile.garuda.components.ResourceManager;
import dav.mobile.garuda.components.SoundManager;
import dav.mobile.garuda.states.game.objects.Garuda;

public class Bunga extends Encounter {
	//Outer bounds
	private float m_BoundWidth;
	private float m_BoundHeight;
	
	//Animation
	private Animation m_Animation;
	
	private boolean m_Animating;
	private float 	m_Time;
	
	public Bunga(int x, int y, int color, Vector2 force) {
		//Super
		super(x, y, color, force);
		
		//Generate texture and animation based on color
		m_Texture = ResourceManager.m_BungaSprite[color][0];
		
		m_Animation = ResourceManager.m_BungaAnimation[color];
		m_Animating = false;
		m_Time = 0.0f;
		
		//Get outer bounds
		m_BoundWidth = 2.0f * m_Texture.getRegionWidth();
		m_BoundHeight = 2.0f * m_Texture.getRegionHeight();
	}

	//Check outer bounds and implisitly act
	@Override
	public boolean DoesAABBOuterHit(Garuda garuda) {
		//Init
		boolean m_Hit = false;
		
		float xMin = getPosX() - m_BoundWidth;
		float yMin = getPosY() - m_BoundHeight;
		float xMax = getPosX() + (float) getWidth() + m_BoundWidth;
		float yMax = getPosY() + (float) getHeight() + m_BoundHeight;
		
		float garudaX = garuda.getPosX() + (float) garuda.getWidth();
		float garudaY = garuda.getPosY() + (float) garuda.getHeight();
		
		//Check collision
		m_Hit = !(xMin>garudaX||xMax<garuda.getPosX()||yMin>garudaY||yMax<garuda.getPosY());
		
		//Go animated if true
		if (m_Hit) {m_Animating = true;}
		
		//Return
		return m_Hit;
	}

	@Override
	public void Update(float deltatime) {
		//Update time if animating
		if (m_Animating) { m_Time += deltatime; }
	}
	
	@Override
	public void Render(SpriteBatch m_Spritebatch) {
		//Check animation
		if (m_Animating) { 
			m_Texture = m_Animation.getKeyFrame(m_Time, true); 
		}
		
		//Draw
		m_Spritebatch.draw(m_Texture, getPosX(), getPosY());
	}

	@Override
	protected void PlaySound() {
		SoundManager.PlaySFX(SoundManager.m_BungaSound);
	}

}
